# -*- coding: utf-8 -*-
import traceback, os, re
import pycommonlib as pyclib
from pymongo    import  Connection
from termcolor  import  cprint

#server1 = '192.168.1.150'
#server2 = 'localhost'
server1 = 'localhost'
server2 = 'beta.mana.vn'
port1   = 27017
port2   = 27017
prefix  = '/home/hoangnamhai/HarvestedData'

conn1    = Connection(server1, port1)
conn2    = Connection(server2, port2)

db1      = conn1['mobile9']
db2      = conn2['my_database']

def getRootLft(name='Ringtones'):
    try:
        root_id = 0; lft = 0
        category    = db2['category']
        result      = category.find_one({'data': name}, {'root_id': 1, 'lft': 1})
        if result!=None:
            root_id = result['root_id']; lft = result['lft']
        return root_id, lft
    except:
        traceback.print_exc()

def getAuthor(name='crawler'):
    try:
        collection    = db2['backend_user']
        result        = collection.find_one({'username': name}, {})
        if result==None: result = USER_COLLECTION.find_one({'username': 'Hindua88'}, {})
        if result!=None:
            return result['_id']
    except:
        traceback.print_exc()

if __name__ == '__main__':
    try:
        root_id, lft = getRootLft()
        if root_id==None or root_id==0: pyclib.forceQuit()
        print root_id, lft
        author  = getAuthor()
        if author==None or author=='':  pyclib.forceQuit()
        print author

        collectionA = db1['ringtones']
        collectionB = db2['article']

        ssh     = pyclib.createSSHClient('mana.vn', 22, 'daudm', '1p%^IRg')
        sftp    = ssh.open_sftp()
        if ssh==None: pyclib.forceQuit()

        result      = collectionA.find()
        for row in result:
            try:
                hashUrl = row['hashUrl']
                cexists = collectionB.find_one({'hashUrl': hashUrl})
                if cexists==None:
                    if not row.has_key('ringtones_key'):
                        row['ringtones_type'] = row['type']
                        row['type'] = 'mp3'
                    if not row.has_key('author_id'):
                        row['author_id'] = author
                    if not row.has_key('root'):
                        row['root'] = root_id
                    if not row.has_key('category'):
                        row['category'] = lft
                    path_file = row['content']['download']['source']
                    path_file = path_file.replace('/uploads/', '')
                    subpath   = path_file[:15] 
                    path_file = '{0}/{1}'.format(prefix, path_file)
                    path_dir  = '{0}/{1}'.format(prefix, subpath)
                    print path_file
                    print path_dir
                    # upload to server mana.vn
                    stdin, stdout, stderr   = ssh.exec_command("mkdir -p -m 0777 {0}".format(path_dir))
                    remote_file = path_file
                    info = pyclib.rexists(sftp, remote_file)
                    if not info:
                        flgRun = False
                        for i in range(0, 3):
                            try:
                                sftp.put(path_file, remote_file)
                                print 'Copy file: ', path_file, ' => ', remote_file
                                flgRun = True; break
                            except:
                               continue 
                        if flgRun: 
                                del row['_id']
                                collectionB.save(row)
                                cprint('Lưu thành công!', 'green')
                        else:   cprint('Không upload được lên server.', 'red')
                    else:
                        cprint('Đã tồn tại trên server', 'red')
                else: cprint('Đã tồn trong cơ sở dữ liệu.', 'red')
            except:
                traceback.print_exc()
                continue
        '''
        '''
        ssh.close()
    except:
        traceback.print_exc()

conn1.end_request()
conn2.end_request()
